package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.widget.Toast;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Util.ThreadPool;

import aclasdriver.AclasScaleUsb;

/**
 * 顶尖os2x型号USB电子秤
 */
public class DingJianOS2X implements IBleControl {
    protected boolean ReadWeight;//是否读取重量
    private final IinfoCallback mCallback;
    private AclasScaleUsb scale;
    private final Context mContext;
    public DingJianOS2X(Context context, IinfoCallback callback) {
        mCallback = callback;
        mContext = context;
    }




    @Override
    public void bleConnect() {
        scale = new AclasScaleUsb(mContext, new AclasScaleUsb.AclasScaleUsbListener() {
            @Override
            public void OnReturnInfo(AclasScaleUsb.RTNCODE rtncode, String s) {
                switch (rtncode){
                    case RTNCODE_DEVICENUM:
                        // 返回过滤后符合要求的usb设备数量设备数量
                        /**
                         * 初始化时唯一返回的状态（扫码到的usb设备），用作连接成功标识
                         * 注意：此时还未初始化完毕，所以等待一秒后回调给外部
                         */
                        ThreadPool.execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(1000);
                                    mCallback.onConnect("连接成功",true);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        break;
                    case RTNCODE_USBUNMOUNT:
                        // usb线被拔掉
                        break;
                    //case RTNCODE_INITSCALESUCCESS:
                        // 初始化成功 只有在获取重量时返回
                       // break;
                    //case RTNCODE_OPENSUCCESS:
                        // 也只有在获取重量时返回
                       // break;
                    case ERRCODE_INITFAIL:
                        // 初始化失败
                        Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void OnRcvWeight(AclasScaleUsb.RTNCODE rtncode, float v, boolean b, String s, int i) {
                if(ReadWeight) {
                    mCallback.onGetWeight(v + "", b);
                }
            }
        });
    }

    @Override
    public void bleDisconnect() {
        if(scale != null)
            ThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    scale.AclasClose();
                }
            });
    }

    @Override
    public void getWeight() {
        ReadWeight = true;
        if(scale != null)
            scale.AclasOpen(0);
    }

    @Override
    public void stopWeight() {
        ReadWeight = false;
    }

    @Override
    public void clearTare() {

    }

    @Override
    public void tare() {
        if(scale != null)
            scale.AclasTare();
    }

    @Override
    public void zero() {
        if(scale != null)
            scale.AclasZero();
    }
}
